#ifndef MODBUSSERIALTEST_H
#define MODBUSSERIALTEST_H

#include <QObject>
#include <QDebug>
#include <QSerialPort>
#include <QTimer>
#include <QQueue>
#include <QByteArray>
#include <HttpRequest.h>


class ModbusSerialTest : public QObject
{
    Q_OBJECT
public:
    ModbusSerialTest(QObject *parent = nullptr);
    ~ModbusSerialTest();

    bool startPort(); // 开始测试

    void stopPort(); // 停止测试

    void closePort(); // 关闭测试

    // 添加数据到队列
    void enqueueData(const QByteArray &data);

    char get_deviceAddress();

    uint16_t ModbusCRC16(const QByteArray &senddata);
    QString toHexString(const QByteArray &byteArray);
    QPair<uint8_t, uint8_t> getHighAndLowByte(const QString &str, const QByteArray &byteArray, const uint8_t index);
    uint16_t getHighAddLowUint16_t(const QString &str, const QByteArray &byteArray, const uint8_t index);
    void scanSerialPorts();
    bool checkConnectState();
    bool getPlcConnectState() const;
    void setPlcConnectState(bool value);

//    bool getRead_3functionCodeFlag() const;
//    void setRead_3functionCodeFlag(bool value);

signals:
    //    void newStatusBit(const QString &data);
    void topPhotoRequest(const uint16_t state);
    void ABCD_PhotoRequest(const uint16_t state);
    void bottomPhotoRequest(const uint16_t state);
    void newStatusBit(const uint16_t data);
//    void shiftOnAStatus(const uint8_t data);

//    void TopSidelightControlsData(const QString sData);
//    void BottomlightControlsData(const QString sData);

    void dataReceived(const QByteArray &data);

    void sensorStateMap(QMap<QString, uint8_t> sensorState);

    void sendDevCurrentGroup(uint8_t devCurrentGroup);
public slots:
    // 发送Modbus请求的函数
    void do_setSendModbusRequest(uint8_t functionCode, uint16_t address, uint16_t value);

    void do_plcSendPulse(uint8_t functionCode, uint16_t address);

//    void do_currentGroupData(const QMap<QString, QString> map_currentGroupData);

    void onReadyRead();
    void onTimeout();
    void onErrorOccurred(QSerialPort::SerialPortError error);

    void do_enqueueData(const QByteArray &data);

private slots:
    // 发送Modbus请求的函数
    void sendModbusRequest(uint8_t functionCode, uint16_t address, uint16_t value = 0);

    // 解析Modbus响应的函数
//    bool parseModbusResponse(const QByteArray &response);
    bool parseModbusResponseTest(const QByteArray &response);

    // 构造Modbus RTU请求帧
    QByteArray constructModbusRequest(uint8_t functionCode, uint16_t address, uint16_t value = 0);

    // 计算Modbus RTU CRC校验
    QPair<uint8_t, uint8_t> calculateCRC(const QByteArray &data);

    //    void sendHttpCsvUploadInfo(QString fileName);
    QString ensureThreeDigits(const QString &input);

private:
    QSerialPort serialPort;
    QTimer timer;
    QByteArray readBuffer, readBuffer_Temp;

    uint8_t plcState = 0; // PLC当前状态
    uint16_t sendCount = 0; // 发送计数
    uint16_t packetLoss = 5; // 丢包率
    bool plcConnectState = false;// PLC端口连接状态


    QQueue<QByteArray> m_dataQueue;  // 发送队列
    const int INTERVAL = 50+50; // 100ms

    uint16_t deviceAddress = 1;// modbus地址

//    enum readState{
//        readPF = 0,
//        readOther
//    }read_3functionCode = readPF;

    bool read_3functionCodeFlag = true;

};

#endif // MODBUSSERIALTEST_H
